/**
 * 
 */
package travibot.ui.gui;

import java.util.Observable;

import org.eclipse.swt.widgets.Display;

import travibot.engine.Session;
import travibot.ui.UIFactory;
import travibot.ui.gui.forms.MainForm;
import travibot.ui.gui.forms.TraviBotShell;
import travibot.ui.gui.notification.StatusNotification;

/**
 * @author tajzivit
 * 
 */
public class GUIFactory extends UIFactory {

    private MainForm mainForm = null;
    private Display display = null;

    public void update(Observable o, Object arg) {

        if (!(arg instanceof Session.State))
            return;

        // get current state
        Session.State state = (Session.State)arg;

        // proceed actions according to state
        if (state.equals(Session.State.CONNECTED)) {
            mainForm.updateCityList(getActiveSession());
        }

        // update status bar text
        mainForm.updateStatusBar(new StatusNotification(getActiveSession().getState(), getActiveSession()
                .getStateDescription(), ""));
        mainForm.updateTasksTab(getActiveSession().getTaskQueue());
    }

    public void startUI() {

        if (mainForm == null && display == null) {
            display = new Display();
            mainForm = new TraviBotShell();
            mainForm.createSShell(display);
            mainForm.setSession(getActiveSession());
            mainForm.setGameSession(getGameSession());
        }

        mainForm.getSShell().open();
        while (!mainForm.getSShell().isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}